﻿using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Foulds.Calculator.Service.Test
{
    /// <summary>
    ///This is a test class for ICalculatorTest and is intended
    ///to contain all ICalculatorTest Unit Tests
    ///</summary>
    [TestClass]
    public class CalculatorTest
    {
        #region Test Context
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }
        #endregion

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        #region Create Calculator
        /// <summary>
        /// Create an instance of the appropriate concrete class.
        /// </summary>
        /// <returns>Returns an implementation of the ICalculator interface.</returns>
        protected virtual ICalculator CreateCalculator()
        {
            ICalculator target = new Calculator();
            return target;
        }
        #endregion

        #region Power On Test
        /// <summary>
        /// Test the initialization of the calculator.
        /// </summary>
        [TestMethod]
        public void PowerOnTest()
        {
            AutoResetEvent waitHandle = new AutoResetEvent(false);

            // an event handler to signal that the finished event was raised
            DisplayChange eventHandler = (inputDisplay, expressionDisplay) => waitHandle.Set();

            ICalculator target = this.CreateCalculator();
            target.DisplayChanged += eventHandler;
            target.PowerOn();

            // Wait until the event handler is invoked
            if (!waitHandle.WaitOne(50000, false))
                Assert.Fail("Test timed out.");

            Assert.AreEqual("0", target.InputDisplay, "An incorrect value is in the input display");
            Assert.AreEqual(string.Empty, target.ExpressionDisplay);
        }
        #endregion
    }
}
